package cn.iocoder.yudao.module.javainfo.controller.admin.productsort;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.javainfo.controller.admin.productsort.vo.ProductSortListReqVO;
import cn.iocoder.yudao.module.javainfo.controller.admin.productsort.vo.ProductSortRespVO;
import cn.iocoder.yudao.module.javainfo.controller.admin.productsort.vo.ProductSortSaveReqVO;
import cn.iocoder.yudao.module.javainfo.controller.admin.productsort.vo.ProductSortSimpleRespVO;
import cn.iocoder.yudao.module.javainfo.dal.dataobject.productsort.ProductSortDO;
import cn.iocoder.yudao.module.javainfo.service.productsort.ProductSortService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 商品分类")
@RestController
@RequestMapping("/javainfo/product-sort")
@Validated
public class ProductSortController {

    @Resource
    private ProductSortService productSortService;

    @PostMapping("create")
    @Operation(summary = "创建商品分类")
    @PreAuthorize("@ss.hasPermission('javainfo:product-sort:create')")
    public CommonResult<Long> createProductSort(@Valid @RequestBody ProductSortSaveReqVO createReqVO) {
        Long productSortId = productSortService.createProductSort(createReqVO);
        return success(productSortId);
    }

    @PutMapping("update")
    @Operation(summary = "更新商品分类")
    @PreAuthorize("@ss.hasPermission('javainfo:product-sort:update')")
    public CommonResult<Boolean> updateProductSort(@Valid @RequestBody ProductSortSaveReqVO updateReqVO) {
        productSortService.updateProductSort(updateReqVO);
        return success(true);
    }

    @DeleteMapping("delete")
    @Operation(summary = "删除商品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('javainfo:product-sort:delete')")
    public CommonResult<Boolean> deleteProductSort(@RequestParam("id") Long id) {
        productSortService.deleteProductSort(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Operation(summary = "批量删除商品分类")
    @Parameter(name = "ids", description = "编号列表", required = true)
    @PreAuthorize("@ss.hasPermission('javainfo:product-sort:delete')")
    public CommonResult<Boolean> deleteProductSortList(@RequestParam("ids") List<Long> ids) {
        productSortService.deleteProductSortList(ids);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取商品分类列表")
    @PreAuthorize("@ss.hasPermission('javainfo:product-sort:query')")
    public CommonResult<List<ProductSortRespVO>> getProductSortList(ProductSortListReqVO reqVO) {
        List<ProductSortDO> list = productSortService.getProductSortList(reqVO);
        return success(BeanUtils.toBean(list, ProductSortRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取商品分类精简信息列表", description = "只包含被开启的商品分类，主要用于前端的下拉选项")
    public CommonResult<List<ProductSortSimpleRespVO>> getSimpleProductSortList() {
        List<ProductSortDO> list = productSortService.getProductSortList(
                new ProductSortListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
        return success(BeanUtils.toBean(list, ProductSortSimpleRespVO.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品分类信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('javainfo:product-sort:query')")
    public CommonResult<ProductSortRespVO> getProductSort(@RequestParam("id") Long id) {
        ProductSortDO productSort = productSortService.getProductSort(id);
        return success(BeanUtils.toBean(productSort, ProductSortRespVO.class));
    }

}

